package com.android.qjpmc.application_module.station.fragment;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.android.qjpmc.R;
import com.android.qjpmc.application_module.contract.RtDataContract;
import com.android.qjpmc.application_module.viewModel.RtDataViewModel;
import com.android.qjpmc.bean.PointParameterBean;
import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.bean.StationDataBean;
import com.android.qjpmc.constant.Constant;
import com.android.qjpmc.data_module.PwrDataModule;
import com.android.qjpmc.data_module.PwrDevice;
import com.android.qjpmc.data_module.PwrStation;
import com.android.qjpmc.databinding.FragementRtDataItemBinding;
import com.android.qjpmc.databinding.FragmentStationRtDataBinding;
import com.android.qjpmc.widget.databindingadapter.BaseBindingItemPresenter;
import com.android.qjpmc.widget.databindingadapter.BaseDataBindingDecorator;
import com.android.qjpmc.widget.databindingadapter.SingleTypeBindingAdapter;
import com.android.qjpmc.widget.mvvm.factory.CreateViewModel;
import com.android.qjpmc.widget.mvvm.view.BaseMVVMFragment;
import com.orhanobut.logger.Logger;

import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Handler;
import android.os.Message;

@CreateViewModel(RtDataViewModel.class)
public class RtDataFragment extends BaseMVVMFragment<RtDataViewModel, FragmentStationRtDataBinding> implements RtDataContract.View, BaseBindingItemPresenter {
    private StationDataBean stationData;
    private String stationId;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_station_rt_data;
    }

    private List<String> data_list;
    private ArrayAdapter<String> arr_adapter;
    private ArrayList<PwrDevice> m_DeviceList = new ArrayList<PwrDevice>();
    private PwrDevice m_thePwrDevice = null;

    PwrStation m_CurrentPwrStation = null;
    PwrDeviceBean m_CurrentDeviceBean = null;

    PwrDevice GetCurrentPwrDevice() {
        return m_thePwrDevice;
    }

    List<PointParameterBean> GetCurrentDeviceParaList() {
        PwrDevice device = GetCurrentPwrDevice();
        if (device == null) {
            return null;
        }
        return device.GetAnalogParametesBeanList();

    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
/*
直接从当前设备更新数据,不从参数bean更新
                if (m_CurrentDeviceBean != null) {
                  //  UpdataValuesText(m_CurrentDeviceBean.pointList);
                    PwrDeviceBean bean = m_thePwrDevice.GetDeviceBean();
                    if (bean != null)
                    {
                        UpdataValuesText(bean.pointList);
                    }
                  //  mViewModel.getPointDataOfGroup(stationId, m_CurrentDeviceBean);
                }

 */
                List<PointParameterBean> paraList = GetCurrentDeviceParaList();
                UpdataValuesText(paraList);

            }
            super.handleMessage(msg);
        }

        ;

    };
    Timer m_RtDataTimer = null;
    ;

    TimerTask m_task = null;

    int StartTimer() {
        if (m_task != null) {
            m_task.cancel();
        }
        m_task = new TimerTask() {
            @Override
            public void run() {
                // 发送消息
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }

        };

        m_RtDataTimer = new Timer();
        if (m_RtDataTimer != null) {
            m_RtDataTimer.schedule(m_task, 1000, 1000); // 1s后执行task,经过1s再次执行
        }
        return Constant.NO_ERROR;
    }

    @Override
    public void initView() {
        if (stationData.stationGroupList != null && stationData.stationGroupList.size() > 0) {
            mViewModel.getPointDataOfGroup(stationId, stationData.stationGroupList.get(0));
        }
        List<Integer> images = new ArrayList<>();
        images.add(R.drawable.ic_action_name_1);
        images.add(R.drawable.ic_action_name_2);
        images.add(R.drawable.afc_button_create_folder);
        images.add(R.drawable.ic_action_name_4);
        mBinding.setView(this);
        View view_index = mBinding.layoutTitle.findViewById(R.id.index);
        if (view_index != null) {
            ((TextView) view_index).setGravity(Gravity.CENTER);

        }
        View view_name = mBinding.layoutTitle.findViewById(R.id.name);
        if (view_name != null) {
            ((TextView) view_name).setGravity(Gravity.CENTER);
        }
        View view_value = mBinding.layoutTitle.findViewById(R.id.value);
        if (view_value != null) {
            ((TextView) view_value).setGravity(Gravity.CENTER);
        }
        View view_unit = mBinding.layoutTitle.findViewById(R.id.value);
        if (view_unit != null) {
            ((TextView) view_unit).setGravity(Gravity.CENTER);
        }
        m_CurrentPwrStation = PwrDataModule.getInstance().GetCurrentStationNode();
        //数据
        data_list = new ArrayList<String>();
        if (m_CurrentPwrStation == null) {

        } else {
            PwrDevice Device = m_CurrentPwrStation.GetFirstDevice();
            m_thePwrDevice = Device;
            while (Device != null) {
                data_list.add(Device.getM_Name());
                m_DeviceList.add(Device);
                Device = Device.GetNext();
            }
        }
        data_list.clear();
        for (PwrDeviceBean pwrDeviceBean : stationData.stationGroupList) {
            data_list.add(pwrDeviceBean.deviceName);
        }
        //适配器
        arr_adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, data_list);
        //设置样式
        arr_adapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
        //加载适配器
        mBinding.deviceSpinner.setAdapter(arr_adapter);
        mBinding.deviceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.d("mark", "onItemSelected() is invoked!===" + position);
                parent.setVisibility(View.VISIBLE);
                if (m_DeviceList != null) {
                    if (position < m_DeviceList.size()) {
                        m_thePwrDevice = m_DeviceList.get(position);
                    } else {
                        m_thePwrDevice = null;
                    }
                    PwrDataModule.getInstance().m_RtDataEntranceMng.SetCurrentRtDataEntrance(m_thePwrDevice);

                }
                m_CurrentDeviceBean = stationData.stationGroupList.get(position);
                if (m_CurrentDeviceBean != null) {
                    if (!m_CurrentDeviceBean.isGetPointReamTimeData) {
                        mViewModel.getPointDataOfGroup(stationId, m_CurrentDeviceBean);
                    } else {
                        UpdataValuesText(m_CurrentDeviceBean.pointList);
                    }
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        StartTimer();

    }

    void UpdataValuesText(List<PointParameterBean> pointList) {
        if (pointList != null && pointList.size() != 0) {

            /*暂时屏蔽 2022年4月17日17:52:47
            //  if ((m_thePwrDevice != null) && (m_CurrentPwrStation != null)) {
            //ArrayList<AnalogDataItem> AnalogList = PwrDataModule.getInstance().GetAnalogDataList(m_CurrentPwrStation.getM_ID(), m_thePwrDevice.GetID());
            */

            SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(getContext(), pointList, R.layout.fragement_rt_data_item);
            mBinding.rtDataList.setAdapter(adapter);

            adapter.setItemPresenter(this);

            mBinding.rtDataList.setLayoutManager(new LinearLayoutManager(getContext()));


            adapter.setItemDecorator(new BaseDataBindingDecorator<PointParameterBean, FragementRtDataItemBinding>() {
                @Override
                public void decorator(FragementRtDataItemBinding binding, int position, int viewType, PointParameterBean data) {
                    if (data == null) {
                        return;
                    }
                    binding.tvName.setText(data.propsName);
                    if (data.analogUnit == null) {
                        binding.tvUnit.setText("");
                    } else {
                        binding.tvUnit.setText(data.analogUnit);
                    }
                    if (data.pointData != null) {
                        if (data.pointData.value == null) {
                            binding.tvValue.setText("---");
                        } else {
                            if (data.pointData == null) {
                                System.out.println("rt data is null!");
                            } else {
                                binding.tvValue.setText(data.pointData.value);
                            }
                        }
                    } else {
                        System.out.println("rt data is null!");
                    }

                    binding.tvIndex.setText(Integer.toString(position + 1));
                    binding.tvName.setGravity(Gravity.CENTER);
                    binding.tvUnit.setGravity(Gravity.CENTER);
                    binding.tvValue.setGravity(Gravity.CENTER);
                    binding.tvIndex.setGravity(Gravity.CENTER);
                    if (position % 2 == 0) {
                        binding.tvName.setBackgroundColor(Constant.COLOR_TEXT_BK_GREEN);
                        binding.tvUnit.setBackgroundColor(Constant.COLOR_TEXT_BK_GREEN);
                        binding.tvValue.setBackgroundColor(Constant.COLOR_TEXT_BK_GREEN);
                        binding.tvIndex.setBackgroundColor(Constant.COLOR_TEXT_BK_GREEN);
                    } else {
                        binding.tvName.setBackgroundColor(Constant.COLOR_TEXT_BK_BLUE);
                        binding.tvUnit.setBackgroundColor(Constant.COLOR_TEXT_BK_BLUE);
                        binding.tvValue.setBackgroundColor(Constant.COLOR_TEXT_BK_BLUE);
                        binding.tvIndex.setBackgroundColor(Constant.COLOR_TEXT_BK_BLUE);
                    }
/*
                    binding.tvIndex.setWidth(2);
                    binding.tvName.setWidth(10);
                    binding.tvValue.setWidth(10);
                    binding.tvUnit.setWidth(10);
*/
                }
            });
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void onItemClick(int position, Object itemData) {

    }

    public void initStationData(String stationId, StationDataBean data) {
        this.stationData = data;
        this.stationId = stationId;
    }

    @Override
    public void returnGroupPointList(PwrDeviceBean stationGroupData) {
        Logger.d("groupPointList" + stationGroupData.toString());

        //   arr_adapter.notifyDataSetChanged();
        UpdataValuesText(stationGroupData.pointList);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        m_RtDataTimer.cancel();
    }
}
